package com.gin.no_gay_alliance.viewmodel;

import com.gin.nga.bbscode.entity.BbsTag;
import com.gin.nga.enums.FromClient;
import com.gin.nga.enums.ReplyStatus;
import com.gin.nga.response.alterlog.LastEdit;
import com.gin.nga.response.alterlog.ReputationLog;
import com.gin.nga.response.field.AnonymousUser;
import com.gin.nga.response.field.AvatarBuff;
import com.gin.nga.response.field.Gift;
import com.gin.nga.response.field.LabelValue;
import com.gin.nga.response.field.Medal;
import com.gin.nga.response.field.ReplyInfo;
import com.gin.nga.response.field.ReplySimple;
import com.gin.nga.response.field.user.UserGroup;
import com.gin.nga.response.field.user.UserInfoContext;
import com.gin.nga.response.field.user.UserInfoRead;
import com.gin.nga.response.vote.BaseVoteData;
import com.gin.no_gay_alliance.database.entity.ReplyHistory;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 一条回复
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/14 17:09
 **/
public class ReplyItem {
    /**
     * 正文节点
     */
    public List<BbsTag> nodes;
    /**
     * 礼物，key =id value = 数量
     */
    public List<Gift> gifts;
    /**
     * 最后编辑时间
     */
    public LastEdit lastEdit;
    /**
     * 操作日志
     */
    public List<ReputationLog> logs;
    /**
     * 附件信息
     */
    public ArrayList<AttachmentModel> attachments;
    /**
     * 热评
     */
    public List<ReplyItem> hotReplies;
    /**
     * 贴条
     */
    public List<ReplyItem> comment;
    /**
     * 使用的客户端,PC,IOS,安卓
     */
    public FromClient fromClient;
    /**
     * 楼层号
     */
    public Integer floorNumber;
    /**
     * 发表日期
     */
    public ZonedDateTime postDatetime;
    /**
     * 赞数
     */
    public Integer agreeCount;
    /**
     * 投票数据
     */
    public BaseVoteData voteData;
    /**
     * 作者uid
     */
    public Long authorUid;
    /**
     * 回复正文
     */
    public String content;
    /**
     * 回复id
     */
    public Long replyId;
    /**
     * 回复或引用的id
     */
    public Long replyToId;
    /**
     * 标题
     */
    public String title;
    /**
     * 主题id
     */
    public Long topicId;
    /**
     * 状态
     */
    public List<ReplyStatus> statuses;
    /**
     * 用户名
     */
    public String username;
    /**
     * 是否匿名
     */
    public boolean anonymous;

    // 用户信息 起

    /**
     * 声望等级+声望值
     */
    public LabelValue reputation;
    /**
     * 随机头像
     */
    public String avatar;
    /**
     * 徽章信息
     */
    public List<Medal> medals;
    /**
     * 用户组
     */
    public UserGroup group;
    /**
     * 头像buff
     */
    public AvatarBuff avatarBuff;
    /**
     * 威望
     */
    public Integer prestige;
    /**
     * 发帖数
     */
    public Integer postCount;
    /**
     * 版面ID
     */
    public Long forumId;

    /**
     * 手动控制是否显示评论
     */
    public boolean showComment = true;

    /**
     * 手动控制是否显示热门回复
     */
    public boolean showHotReplies = true;
    /**
     * 修改记录（原始数据）
     */
    public String alterinfoRaw;

    /**
     * 历史版本
     */
    public List<ReplyHistory> versions;

    // 用户信息 止
    public ReplyItem() {
    }

    public ReplyItem(ReplySimple data) {
        this.authorUid = data.getAuthorUid();
        this.content = data.getContent();
        this.replyId = data.getReplyId();
        this.replyToId = data.getReplyToId();
        this.postDatetime = data.getPostDatetime();
        this.title = data.getTitle();
        this.topicId = data.getTopicId();
        this.statuses = data.getStatus();
        this.nodes = data.getContentNodes();
    }

    public ReplyItem(ReplyInfo data) {
        this((ReplySimple) data);

        if (data.getAlterInfo() != null) {
            this.lastEdit = data.getAlterInfo().getLastEdit();
            this.logs = data.getAlterInfo().getLogs();
            this.alterinfoRaw = data.getAlterInfo().getRaw();
        }
        if (data.getAttachments() != null) {
            this.attachments = data.getAttachments().values().stream()
                    .map(AttachmentModel::new)
                    .collect(Collectors.toCollection(ArrayList::new));
        }

        if (data.getHotReplies() != null) {
            this.hotReplies = data.getHotReplies().values().stream().map(ReplyItem::new).collect(Collectors.toList());
            this.hotReplies.stream().filter(i -> Objects.equals(i.floorNumber, 0)).forEach(i -> i.floorNumber = null);
        }
        if (data.getComment() != null) {
            this.comment = data.getComment().values().stream().map(ReplyItem::new).collect(Collectors.toList());
            this.comment.stream().filter(i -> Objects.equals(i.floorNumber, 0)).forEach(i -> i.floorNumber = null);
        }
        this.gifts = data.getGifts();

        this.fromClient = data.getFromClient();
        this.floorNumber = data.getFloorNumber();
        this.agreeCount = data.getAgreeCount();
        this.voteData = data.getVoteData();
    }

    public void setUserInfo(Object data) {
        if (data instanceof UserInfoContext) {
            UserInfoContext context = (UserInfoContext) data;
            this.reputation = context.getReputation();
            this.avatar = context.getAvatar();
            this.medals = context.getMedals();
            this.group = context.getGroup();
            this.avatarBuff = context.getAvatarBuff();
//            this.userInfo = context.getUserInfo();

            this.anonymous = false;
            UserInfoRead userInfo = context.getUserInfo();
            if (userInfo != null) {
                this.prestige = userInfo.getPrestige();
                this.username = userInfo.getUsername();
                this.postCount = userInfo.getPostCount();
            }
        }
        if (data instanceof AnonymousUser) {
            AnonymousUser anonymousUser = (AnonymousUser) data;
            this.anonymous = true;
            this.username = anonymousUser.getNickname();
        }
    }
}
